package com.secondGroup.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.secondGroup.entity.ElectronicAccount;
import com.secondGroup.entity.Flow;
import com.secondGroup.entity.User;
import com.secondGroup.service.ElectronicAccountService;
import com.secondGroup.service.FlowService;
import com.secondGroup.service.UserService;
import com.secondGroup.vo.StringVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ws
 * @since 2022-05-05
 */
@Controller
@RequestMapping("/flow")
public class FlowController {
    @Autowired
    UserService userService;
    @Autowired
    FlowService flowService;
    @Autowired
    ElectronicAccountService electronicAccountService;

    @RequestMapping("/transferAccount")
    public String toTransferAccount(String uid , HttpSession session){
            return "transferAccount";
    }

    /**
     * 通过eid获取用户的
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping("/getEName")
    public String getEName(String account){
        User user = userService.getOne(new QueryWrapper<User>()
                .lambda()
                .eq(User::getAccount, account));
        if(user==null){
            return "不存在";
        }
        if(user.getEId()==null){
            return "没有开通电子账户";
        }
        String toEid=user.getEId();
        ElectronicAccount one = electronicAccountService.getOne(new QueryWrapper<ElectronicAccount>()
                .lambda()
                .eq(ElectronicAccount::getEId, toEid)
                .select(ElectronicAccount::getEName));

        return one.getEName();
    }

    /**
     * 转账操作
     * @param
     * @param amount
     * @param session
     * @return
     */
    @RequestMapping("/transferAccountForm")
    public String transferAccountForm(String account , BigDecimal amount , HttpSession session, String payPassword, Model model){
        //先通过对方的用户名获取到对方的电子账户id
        User user = userService.getOne(new QueryWrapper<User>()
                .lambda()
                .eq(User::getAccount, account));
        String toEid=user.getEId();
        User userInfo = (User) session.getAttribute("userInfo");
        //调用转账业务
        StringVo stringVo = flowService.transferAccount(userInfo,amount,toEid,payPassword);
        if(stringVo.getI()==1){
            model.addAttribute("toName",stringVo.getS());
            model.addAttribute("amount",amount);
            return "transferAccountSuccess";
        }if(stringVo.getI()==2) {
            model.addAttribute("value",2);
        }else{
            model.addAttribute("value",3);
        }
        return "transferAccountError";
    }

    /**
     *
     * @return
     */
    @RequestMapping("/getFlow")
    public String getFlow(HttpSession session ,Model model ,String pageNum){
        User user = (User) session.getAttribute("userInfo");
        Page<Flow> defaultFlow = flowService.getDefaultFlow(user,pageNum);
        model.addAttribute("page",defaultFlow);
        model.addAttribute("flows",defaultFlow.getRecords());

        return "showFlow";
    }

    /**
     *  * tradeTime: 2021
     *      * tradeTime: 1
     *      * tradeTime: 1
     *      * tradeType: 转账
     * @return
     */
    @RequestMapping("/getFlowByT")

    public String getFlowByT(HttpSession session ,int[] tradeTime , Integer tradeType,Model model){
        User user = (User) session.getAttribute("userInfo");

        if(tradeTime!=null&&tradeType!=null){
            List<Flow> flows = flowService.getFlowByT(user, tradeTime, tradeType);
            model.addAttribute("flows",flows);
            return "showFlowByT";

        }return "error";


    }
}
